﻿/*  
	------------------------------------------------------------------------
	 NQ Core Library
	
	 Homepage: http://www.awzhome.de/
	------------------------------------------------------------------------
	
	This Source Code Form is subject to the terms of the Mozilla Public
	License, v. 2.0. If a copy of the MPL was not distributed with this
	file, You can obtain one at http://mozilla.org/MPL/2.0/.
   
	The Original Code is code of NQ Core Library.

	The Initial Developer of the Original Code is Andreas Weizel.
	Portions created by the Initial Developer are
	Copyright (C) 2012 Andreas Weizel. All Rights Reserved.
	
	Contributor(s): (none)
	
	------------------------------------------------------------------------
*/

using System;

namespace AWZhome.NQ.Core.Fluent
{
	/// <summary>
	/// Wraps a list of service instances to be used with the NQ fluent interface.
	/// </summary>
	/// <typeparam name="T">Type of the service instances.</typeparam>
	public class ServiceListWrapper<T>
	{
		protected T[] _serviceInstances = null;

		internal ServiceListWrapper(T[] serviceInstances)
		{
			_serviceInstances = serviceInstances;
		}

		protected ServiceListWrapper()
		{
		}

		/// <summary>
		/// Returns a service instance from the internal list.
		/// </summary>
		/// <param name="index">Index of the service instance to retrieve.</param>
		/// <returns>Service instance at the given index.</returns>
		public virtual T this[int index]
		{
			get
			{
				return _serviceInstances[index];
			}
		}

		/// <summary>
		/// Returns an array of the wrapped service instances.
		/// </summary>
		//[Obsolete("Please use Instances property instead.")]
		public virtual T[] To
		{
			get
			{
				return _serviceInstances;
			}
		}

		/// <summary>
		/// Returns an array of the wrapped service instances.
		/// </summary>
		public virtual T[] Instances
		{
			get
			{
				return _serviceInstances;
			}
		}

		/// <summary>
		/// Invokes (calls the invoke method of) all wrapped service instance.
		/// </summary>
		/// <returns>The instance of the wrapper.</returns>
		public virtual ServiceListWrapper<T> InvokeAll()
		{
			if (_serviceInstances != null)
			{
				for (int i = 0; i < _serviceInstances.Length; i++)
				{
					if (_serviceInstances[i] != null)
					{
						NQServiceManager.Instance.InvokeService(_serviceInstances[i]);
					}
				}
			}

			return this;
		}

		/// <summary>
		/// Quits (calls the quit method of) all wrapped service instance.
		/// </summary>
		/// <returns>The instance of the wrapper.</returns>
		public virtual ServiceListWrapper<T> QuitAll()
		{
			if (_serviceInstances != null)
			{
				for (int i = 0; i < _serviceInstances.Length; i++)
				{
					if (_serviceInstances[i] != null)
					{
						NQServiceManager.Instance.QuitService(_serviceInstances[i]);
					}
				}
			}

			return this;
		}

		/// <summary>
		/// Executes an expression on all wrapped service instances.
		/// </summary>
		/// <param name="expression">Expression to execute on every instance.</param>
		/// <returns>The instance of the wrapper.</returns>
		public virtual ServiceListWrapper<T> WithAll(Func<T, object> expression)
		{
			if (_serviceInstances != null)
			{
				for (int i = 0; i < _serviceInstances.Length; i++)
				{
					if (_serviceInstances[i] != null)
					{
						expression(_serviceInstances[i]);
					}
				}
			}

			return this;
		}
	}
}
